<?php
/**
 * Created by xiaosong
 * E-mail:4155433@gmail.com
 * Date: 2018/12/6
 * Time: 9:51
 * 点单房间模块
 */

namespace app\api\controller;

use app\common\model\Users;
use Carbon\Carbon;
use think\Db;
use wheat\Wheat;

class Order extends Radio
{



    public function _initialize()
    {
        self::$roomType = [3];
        parent::_initialize();
    }

    /**
     * 上麦、换麦、抱麦
     * */
    public function upWheat(){

        $post = request()->only(['wheat_id','type','user_id'],'post');
        //验证数据
        $result = $this->validate($post,'Wheat.up');
        if(true !== $result){
            api_return(0,$result);
        }

        if(!empty($post['user_id'])){
            $user_id = dehashid($post['user_id']);
            if (!is_numeric($user_id)) api_return(0,'参数错误');
        }else{
            $user_id = $this->user_id;
        }

        $wheat = new Wheat();
        if(isset($post['type']) && $post['type']  == 1){//抱麦

            $this->checkPower($this->user_id);

            if ($post['wheat_id'] == 1){
                api_return(0,'接待位不能抱麦');
            }

            $ret = $wheat->embrace($post['user_id'],self::$room_id,$post['wheat_id']);
        }else{//上麦

            $powerCode = $this->getPowerCode();

//            switch ($powerCode){
//                case 1:
//                    api_return(0,'房主不能上其它麦位');
//                    break;
//                case 2:
//                case 3:
//                    if ($post['wheat_id'] == 1){
//                        api_return(0,'房主位不能上麦');
//                    }
//                    break;
//                default:
                    if (self::$is_free){
                        if (in_array($post['wheat_id'],self::$generalNotUp)){
                            if (!$powerCode){
                                api_return(0,'非管理员不能上麦');
                            }
                        }
                    }else{
                        if ($post['wheat_id'] != 9){
                            api_return(0,'观众只能上老板位,其它麦位需申请');
                        }
                    }
//                    break;
//            }
            $ret = $wheat->on($this->user_id,self::$room_id,$post['wheat_id'],$powerCode);
        }
        if($ret['code']){

            //用户上麦成功 且在排麦中 清除排麦数据
            $type = $post['wheat_id'] == 9 ?1:2;
            $rows = cache('wheatRequest_'.$type.self::$room_id)??[];
            if (in_array($user_id,$rows)){
                unset($rows[$user_id]);
                cache('wheatRequest_'.$type.self::$room_id,$rows,todayEndTime());
            }
            $this->refreshWheat(self::$room_id,$ret['data']['wheat']);
            api_return(1,$ret['msg'],$ret['data']['wheat']);
        }else{
            api_return(0,$ret['msg']);
        }
    }

//    /**
//     * Created by xiaosong
//     * E-mail:4155433@gmail.com
//     * 选项
//     */
//    public function option()
//    {
//        $wheat = new Wheat();
//        $wheatInfo = $wheat->getWheat(self::$room_id);
//        $data['show_house'] = $wheatInfo[0]['is_show'];
//        $data['show_admit'] = $wheatInfo[1]['is_show'];
//        $data['show_boss']  = $wheatInfo[8]['is_show'];
//        $data['show_rank']  = cache('rank_'.self::$room_id)??0;
//        $data['is_free']    = self::$is_free;
//        api_return(1,'获取成功',$data);
//    }
//
//    /**
//     * Created by xiaosong
//     * E-mail:4155433@gmail.com
//     * 选项状态切换
//     */
//    public function changeOption()
//    {
//        $this->checkPower();
//
//        $data = input('post.');
//        if ($data['is'] != 1 && !is_zero($data['is']) ){
//            api_return(0,'状态错误');
//        }
//        $index = 'nothing';
//        switch ($data['type']){
//            case 'rank':
//                $result = cache('rank_'.self::$room_id,$data['is']);
//                break;
//            case 'free':
//                $result = Db::name('room')->where('room_id',self::$room_id)->update(['is_free'=>$data['is']]);
//                break;
//            case 'house':
//                $index = 0;
//                break;
//            case 'admit':
//                $index = 1;
//                break;
//            case 'boss':
//                $index = 8;
//                break;
//            default:
//                api_return(0,'类型错误');
//                break;
//        }
//
//        if (is_numeric($index)){
//            $class = new Wheat();
//            $wheat = $class->getWheat(self::$room_id);
//            $wheat[$index]['is_show'] = $data['is'];
//            $result = $class->setWheat(self::$room_id,$wheat);
//        }
//
//        if ($result){
//            api_return(1,'操作成功');
//        }
//        api_return(0,'操作失败');
//
//    }

    /**
     * Created by xiaosong
     * E-mail:4155433@gmail.com
     * 上麦请求列表
     */
    public function wheatRequestList()
    {
        $type = $this->typeCheck();

        $rows = cache('wheatRequest_'.$type.self::$room_id);

        $map['a.user_id'] = ['in',$rows];
        $map['e.room_id'] = self::$room_id;
        $map['e.online_status'] = 1;
        $model = new Users();

        $rows = $model->_list($map);

        api_return(1,'获取成功',$rows);

    }

    /**
     * Created by xiaosong
     * E-mail:4155433@gmail.com
     * 上麦请求
     */
    public function wheatRequest()
    {
        $this->ApiLimit(3,$this->user_id);

        $type = $this->typeCheck();

        $rows = cache('wheatRequest_'.$type.self::$room_id)??[];

        if (in_array($this->user_id,$rows)) api_return(0,'您正在排队中!');

        $rows[$this->user_id] = $this->user_id;

        cache('wheatRequest_'.$type.self::$room_id,$rows,todayEndTime());

        $this->destroyLimit($this->user_id);

        api_return(1,'正在排队中');

    }

    /**
     * Created by xiaosong
     * E-mail:4155433@gmail.com
     * 上麦请求类型判断
     */
    protected function typeCheck()
    {
        $type = input('post.type');

        //type 1 老板位  2 其它位
        if ($type != 1 && $type != 2) api_return(0,'类型错误');

        return $type;
    }
    
    
    /**
     * Created by xiaosong
     * E-mail:4155433@gmail.com
     * 取消排麦申请
     */
    public function cancelRequest()
    {
        $this->ApiLimit(3,$this->user_id);

        $type = $this->typeCheck();

        $user_id = input('post.user_id');

        if ($user_id){
            $user_id = dehashid($user_id);
            if (!is_numeric($user_id)){
                api_return(0,'用户id错误');
            }else{
                $powerCode = $this->getPowerCode();
                if ($powerCode === 0){
                    api_return(0,'非管理员不能取消排麦请求');
                }
            }
        }else{
            $user_id = $this->user_id;
        }

        $rows = cache('wheatRequest_'.$type.self::$room_id)??[];

        if (!in_array($user_id,$rows)) api_return(0,'未在排队中!');

        unset($rows[$user_id]);

        cache('wheatRequest_'.$type.self::$room_id,$rows,todayEndTime());

        $this->destroyLimit($this->user_id);

        api_return(1,'取消成功');
    }


    /**
     * Created by xiaosong
     * E-mail:4155433@gmail.com
     * 获取点单房间信息
     */
    public function OrderInfo()
    {

        $info = getOrderRoom(self::$room_id);

        api_return(1,'获取成功',$info);

    }






}